

jQuery(function ($) {
    //搜索关键字请求接口渲染页面数据
    var keyword = decodeURIComponent(window.location.href.split('=')[1]);
    var pageOnOff = true;
    if (keyword != 'undefined') {
        //导航栏关键字
        $('#location').append('<span>></span><h1>' + keyword + '</h1>');
    }

    getList(0);
    //分页按钮
    function twbsPagination(res) {
        Page({
			num:Math.ceil(res.totalPage/15),					//页码数
			startnum:1,				//指定页码
			elem:$('#pagination_b'),		//指定的元素
			callback:function(page){	//回调函数
                if (page == 1) {
                    return;
                }
                $('.works_show').empty();
                getList(page - 1);
			}
		});
    }

    function getList(page) {
        $.ajax({
            type: "GET",
            url: search_url + '/search/ask',
            data: {
                keyword: keyword,
                pageNum: page,
                pageSize: 20
            },
            success: function success(res) {
                if (res.code == 200) {
                    var res = res.data;
                    if (res.list.length == 0) {
                        $('#hot ul').append('<h3>\u6CA1\u6709\u6570\u636E</h3>');
                    } else {
                        $('#hot ul').empty();
                        for (var i = 0; i < res.list.length; i++) {
                            res.list[i].content = res.list[i].content.replace(/(\n)/g, "");
                            res.list[i].content = res.list[i].content.replace(/(\t)/g, "");
                            res.list[i].content = res.list[i].content.replace(/(\r)/g, "");
                            res.list[i].content = res.list[i].content.replace(/<\/?[^>]*>/g, "");
                            res.list[i].content = res.list[i].content.replace(/\s*/g, "");
                            res.list[i].content = res.list[i].content.replace(/\&nbsp;*/g, "");
                            $('#hot ul').append('<li class="clearfix">\n<div class="title"><a href="/article-' + res.list[i].id + '.html" target="_blank">'+res.list[i].title+'</a></div><div class="catList"><a href="/question/'+res.list[i].catList[0].askCatUrl+'/" target="_blank">【'+res.list[i].catList[0].askCatName+'】</a></div>   <div class="ask_list_left">\n  <div class="ask_list_title"><span class="ask_list_title"> ' + res.list[i].content + '</span><a href="/article-' + res.list[i].id + '.html" target="_blank">[全文]</a> </div>\n <div class="time_tag"><span class="ask_list_time">\u53D1\u5E03\u4E8E\uFF1A' + (res.list[i].createTime.split('T')[0] + '&nbsp;' + res.list[i].createTime.split('T')[1].split('.')[0]) + '</span></div>\n </div>\n                            </li>');
                        }
                        if (res.total > 20) {
                            //分页按钮
                            if(pageOnOff){
                               twbsPagination(res);
                               pageOnOff=false;
                            }
                        }
                    }
                } else {
                    alert(data.msg);
                }
            },
            error: function error(_error) {
                //请求失败的回调方法 
                alert('网络加载失败，请稍候再试!');
            }
        });
    }
});